
//向页面上填充数据
function fillingData(key){
	 if(checkLowIE()){
		return;
	 }
	 var infos = decodeURIComponent(window.localStorage.getItem('productInfo_' + key));
	 if (infos != null && infos != 'null'){
		 var cookieArr=infos.split("|");
		 cookieArr=cookieArr.reverse();
		 var html="";
		 for(var i=0;i<cookieArr.length; i++){
			 var detailArr=cookieArr[i].split(",");
			 var url=detailArr[0];
			 var productName=detailArr[1];
			 var mUrl=detailArr[2];
			 var productPrice=detailArr[3];
			 var productId=detailArr[4]; 
			 var firstCategory=detailArr[5];
			 var secondCategory=detailArr[6];
			 var type=detailArr[7];
			 
			 var sensorsParam="productClick('"+type+"','浏览历史','"+productId+"','"+productName+"','"+firstCategory+"','"+secondCategory+"','"+productPrice+"','')";
			 
			 html+='<li>';
			 html+='<a onclick="'+sensorsParam+'" class="hisFav-link" href="'+url+'" target="_blank" title="">';
			 html+='<img src="'+mUrl+'" alt="" width="90" height="60"  onerror="showDefImage(this)">';
			 html+='<h4>'+productName+'</h4>';
			 html+='<div class="price"><dfn>&yen;<b>'+productPrice+'</b></dfn>起</div></a></li>';
		 }
		 $("#recentViewed").html(html);
		 $("#recentViewedDiv").show();
	 }
}

//去掉重复的最近游览产品
function filterData(key, productUrl,productName,imgUrl,productPrice,productId,firstCategory,secondCategory,type){
	if(checkLowIE()){
		return;
	}
	 var ids = decodeURIComponent(window.localStorage.getItem('chanpinids_' + key));
	 if(ids == null || ids == 'null'){
		 var productIds="";
		 productIds+=productId;
		 window.localStorage.setItem('chanpinids_' + key, encodeURIComponent(productIds));
		 setCookie(key, productUrl,productName,imgUrl,productPrice,productId,firstCategory,secondCategory,type);
	 }else{
		 productIdsArr=ids.split(",");
		 var isRepeat=false;
		 for(var i=0;i<productIdsArr.length;i++){
			 if(productId==productIdsArr[i]){
				 isRepeat=true;
			 }
		 }
		 if(!isRepeat){
			 if(productIdsArr.length>=5){
				 productIdsArr.shift();
			 }
			 productIdsArr.push(productId);
			 var productIds=productIdsArr.join(",");
			 window.localStorage.setItem('chanpinids_' + key, encodeURIComponent(productIds));
			 setCookie(key, productUrl,productName,imgUrl,productPrice,productId,firstCategory,secondCategory,type);
		 }
	 }
	 fillingData(key);	
}


//添加cookie
function setCookie(key, productUrl,productName,imgUrl,productPrice,productId,firstCategory,secondCategory,type)
{
	 var infos = decodeURIComponent(window.localStorage.getItem('productInfo_' + key));
	 productName = productName.replace(/,/g,'，');
	 productName = productName.replace(/\|/g,'，');
	 if (infos == null || infos == 'null'){
		 var jsonStr="";
		 jsonStr=jsonStr+productUrl+","+productName+","+imgUrl+","+productPrice+","+productId+","+firstCategory+","+secondCategory+","+type;
		 window.localStorage.setItem('productInfo_' + key, encodeURIComponent(jsonStr));
	 }else{
		 productsArr=infos.split("|");
		 if(productsArr.length>=5){
			 productsArr.shift();
		 }
		 products=productsArr.join("|");
		 var jsonStr="|";
		 jsonStr=jsonStr+productUrl+","+productName+","+imgUrl+","+productPrice+","+productId+","+firstCategory+","+secondCategory+","+type;
		 products+=jsonStr;
		 window.localStorage.setItem('productInfo_' + key, encodeURIComponent(products));
	 }
}
function checkLowIE(){
	var version = navigator.appVersion;
	if(version.indexOf('MSIE 7.0') != -1 || version.indexOf('MSIE 6.0') != -1 || version.indexOf('MSIE 5.0') != -1){
		return true;
	}
	return false;
}

function getCookie(cookie_name)
{
    var allcookies = document.cookie;
    var cookie_pos = allcookies.indexOf(cookie_name);   //索引的长度
  
    // 如果找到了索引，就代表cookie存在，
    // 反之，就说明不存在。
    if (cookie_pos != -1)
    {
        // 把cookie_pos放在值的开始，只要给值加1即可。
        cookie_pos += cookie_name.length;//这里容易出问题，所以请大家参考的时候自己好好研究一下
        var cookie_end = allcookies.indexOf(";", cookie_pos);
  
        if (cookie_end == -1)
        {
            cookie_end = allcookies.length;
        }
  
        var value = decodeURIComponent(allcookies.substring(cookie_pos, cookie_end));//这里就可以得到你想要的cookie的值了。。。
    }
    return value;
}

//若加载不到图片，则展示默认图片
function showDefImage(a) {
	a.src="http://pic.lvmama.com/img/cmt/img_300_200.png";
	a.onerror=null;
}

//删除cookie
function delCookie(name)  
{ 
    var cval=getCookie(name); 
    if(cval!=null){
    	document.cookie= name+'=;expires=Thu, 01 Jan 1970 00:00:01 GMT';
    }
} 